package com.icss.servlets;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.icss.beans.AboutUsVO;
import com.icss.beans.TrainStrengthVO;
import com.icss.daos.TrainStrengthDao;

public class TrainStrengthServlet extends HttpServlet {

	public TrainStrengthServlet() {
		super();
	}


	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request,response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		TrainStrengthDao tsdao = new TrainStrengthDao();
		
		String action = request.getParameter("action");
		if("add".equals(action)){
			String title = request.getParameter("strengthTitle");
			String body = request.getParameter("strengthBody");
			
			tsdao.AddTrainStrength(title, body);
			request.getRequestDispatcher("TrainStrengthServlet?action=show").forward(request, response);
		}else if("show".equals(action)){
			ArrayList<TrainStrengthVO> list = tsdao.GetTrainStrength();			
			
			request.setAttribute("list", list);
			request.getRequestDispatcher("/page/index/trainStrength/showTrainStrength.jsp").forward(request, response);
		}else if("display".equals(action)){
			int id = Integer.parseInt(request.getParameter("strengthID")) ;
			
			TrainStrengthVO ts = tsdao.getOneTS(id);
			
			request.setAttribute("TrainStrength", ts);
			request.getRequestDispatcher("/page/index/trainStrength/displayTrainStrength.jsp").forward(request, response);
		}else if("toUpdate".equals(action)){
			int id = Integer.parseInt(request.getParameter("strengthID")) ;
			
			TrainStrengthVO ts = tsdao.getOneTS(id);
			
			request.setAttribute("ts", ts);
			request.getRequestDispatcher("/page/index/trainStrength/updateTrainStrength.jsp").forward(request, response);
		}else if("update".equals(action)){
			int id = Integer.parseInt(request.getParameter("strengthID")) ;
			String aboutTitle=request.getParameter("strengthTitle");
			String body = request.getParameter("strengthBody");
			TrainStrengthVO tsvo = new TrainStrengthVO();
			tsvo.setStrengthBody(body);
			tsvo.setStrengthID(id);
			tsvo.setStrengthTitle(aboutTitle);
			if(tsdao.UpdateTrainStrength(tsvo)){
				request.getRequestDispatcher("TrainStrengthServlet?action=display&strengthID="+id).forward(request, response);
			}
		}else if ("delete".equals(action)){
			int id = Integer.parseInt(request.getParameter("strengthID")) ;
			if(tsdao.DeleteTrainStrength(id)){
				request.getRequestDispatcher("TrainStrengthServlet?action=show").forward(request, response);
			}
		}else if("deleteAll".equals(action)){
			String auidlist = request.getParameter("auidList");

			if (tsdao.deleteAllSelect(auidlist)) {
				request.getRequestDispatcher("TrainStrengthServlet?action=show").forward(request, response);
			}
		}else if("showTrainStrength".equals(action)){
			ArrayList<TrainStrengthVO> list = tsdao.GetTrainStrength();			
			
			request.setAttribute("list", list);
			request.getRequestDispatcher("/page/index/trainStrength/trainStrength.jsp").forward(request, response);
		}
		tsdao.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occure
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
